<?php
/**
 * Created by PhpStorm.
 * User: insgeek
 * Date: 2017/2/10
 * Time: 16:29
 * 单例模式1
 */
namespace DesignPattern\Singleton;

class Singleton
{
    private static $instance;
    private  $ins;

    private function __construct()
    {
        $this->ins = mt_rand(1000, 9999);
    }

    private function __clone()
    {
        // TODO: Implement __clone() method.
        //禁止克隆
        throw new \Exception('prevent clone', 1);
    }

    private function __wakeup()
    {
        // TODO: Implement __wakeup() method.
        //禁止序列化
    }

    public static function getInstance():Singleton
    {
        if (null == static::$instance) {
            static::$instance = new self();
        }

        return static::$instance;
    }

}

$class1 = Singleton::getInstance();
print_r($class1);
$class2 = Singleton::getInstance();
print_r($class2);
print_r($class2 === $class1);